// pages/cart/invoice/invoice.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        needInvoice: false,
        invoiceType: 1, // 1个人，2公司
        invoiceTitle: '',
        invoiceCode: ''
    },

    onLoad: function () {
        wx.getStorage({
            key: "invoice",
            success: res => {
                this.setData({
                    needInvoice: res.data.needInvoice,
                    invoiceType: res.data.invoiceType,
                    invoiceTitle: res.data.invoiceInfo.invoiceTitle || '',
                    invoiceCode: res.data.invoiceInfo.invoiceCode || ''
                })
            }
        })
    },

    //是否需要发票
    isneed(e) {
        let index = e.currentTarget.dataset.index
        this.setData({
            needInvoice: index == 1 ? true : false
        })
    },

    //选择发票类型
    choosetype(e) {
        let index = e.currentTarget.dataset.index
        this.setData({
            invoiceType: index
        })
    },

    //提交确定按钮
    confirminvoice(e) {
        var data = {
            needInvoice: this.data.needInvoice,
            invoiceType: this.data.needInvoice ? this.data.invoiceType : 1,
            invoiceInfo: e.detail.value
        }
        if (data.needInvoice) {
            if ((data.invoiceType == 1 && data.invoiceInfo.invoiceTitle == "") || (data.invoiceType == 2 && (data.invoiceInfo.invoiceTitle == "" || data.invoiceInfo.invoiceCode == ""))) {
                wx.showToast({
                    title: "请填写发票信息^_^",
                    icon: "none"
                })
                return
            }
        }
        wx.setStorageSync('invoice', data)
        wx.navigateBack()
    }
})